﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BBPA.Model;

namespace BBPA.Admin.Sys.Orders.GetMoney
{
    public partial class checkorder : FormPage
    {
        BLL.Orders bll = new BLL.Orders();
        Model.Orders model = new Model.Orders();
        //
        BLL.V_Orders_Goods bllV_Orders_Goods = new BLL.V_Orders_Goods();
        BLL.Orders_Goods bllOrders_Goods = new BLL.Orders_Goods();
        //
        BLL.Goods bllGoods = new BLL.Goods();
        BLL.Supplier bllSupplier = new BLL.Supplier();
        BLL.Orders_State_Log bllOrders_State_Log = new BLL.Orders_State_Log();
        //
        List<Model.V_Orders_Goods> listModelV_Orders_Goods = new List<Model.V_Orders_Goods>();
        //
        /// <summary>
        /// 实际联盟采购价格合计
        /// </summary>
        protected decimal ActualBuyPriceSum = 0;
        protected string IsDelivery = "", IsDeliveryStr = "采购";
        decimal thisSupplier_ExpectBuyPriceSum = 0, thisSupplier_ActualBuyPriceSum = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            IsDelivery = DataTypePlus.requestString(Request.QueryString["IsDelivery"]);
            if (IsDelivery == "0") IsDeliveryStr = "收货";
        }
        /// <summary>
        /// 初始化本页Model
        /// </summary>
        public override void Page_Load_DefaultModel() { model = bll.GetModel(Master.id); }
        /// <summary>
        /// 初始化表单
        /// </summary>
        public override void Form_Default()
        {
            Master.HideSubmit();
        }
        /// <summary>
        /// 绑定表单数据【修改时用】
        /// </summary>
        public override void Form_Bind()
        {
            rp_Goods_Bing();
            ((Label)rp_Goods.Controls[rp_Goods.Controls.Count - 1].FindControl("lb_ExpectPay")).Text = StringPlus.showMoney(model.ExpectPay);
        }
       

        void rp_Goods_Bing()
        {
            ActualBuyPriceSum = 0;
            string sql = "OrdersID=" + Master.id;

            if (IsDelivery != "") sql += " and IsDelivery=" + IsDelivery;

            sql += " order by IsDelivery desc,SupplierID, HasBuy";
            listModelV_Orders_Goods = bllV_Orders_Goods.GetModelList(sql);

            rp_Goods.DataSource = listModelV_Orders_Goods;
            rp_Goods.DataBind();
            Master.ScriptManager_js("Event_Amount_change();");
        }
        protected void rp_Goods_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            int prevSupplierID = 0;
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Model.V_Orders_Goods rowData = (Model.V_Orders_Goods)e.Item.DataItem;
                //
                //供货商
                if (e.Item.ItemIndex > 0 && DataTypePlus.requestInt(rowData.SupplierID) == DataTypePlus.requestInt(listModelV_Orders_Goods[e.Item.ItemIndex - 1].SupplierID))
                {
                    e.Item.FindControl("tr_SupplierName").Visible = false;
                }
                prevSupplierID = DataTypePlus.requestInt(rowData.SupplierID);
                //
                TextBox tb_ActualAmount = (TextBox)e.Item.FindControl("tb_ActualAmount");
                tb_ActualAmount.Text = rowData.ActualAmount.ToString();
                if (!rowData.HasBuy && rowData.ActualAmount == 0) tb_ActualAmount.Text = rowData.ExpectAmount.ToString();

                //小计
                decimal thisGoods_ActualBuyPriceSum = rowData.IsLatePay ? 0 : rowData.HasBuy ? rowData.ActualBuyPriceSum : BBPA.BLL.Goods.GetBuyPriceSum(rowData.GoodsID, rowData.ActualAmount);
                ((Label)e.Item.FindControl("lb_Goods_ActualBuyPriceSum")).Text = StringPlus.showCoin(thisGoods_ActualBuyPriceSum);
                thisSupplier_ExpectBuyPriceSum += rowData.ExpectBuyPriceSum;
                thisSupplier_ActualBuyPriceSum += thisGoods_ActualBuyPriceSum;
                if (e.Item.ItemIndex == listModelV_Orders_Goods.Count - 1 || (e.Item.ItemIndex < listModelV_Orders_Goods.Count - 1 && DataTypePlus.requestInt(rowData.SupplierID) != DataTypePlus.requestInt(listModelV_Orders_Goods[e.Item.ItemIndex + 1].SupplierID)))
                {
                    ((Label)e.Item.FindControl("lb_Supplier_ExpectBuyPriceSum")).Text = StringPlus.showCoin(thisSupplier_ExpectBuyPriceSum);
                    ((Label)e.Item.FindControl("lb_Supplier_ActualBuyPriceSum")).Text = StringPlus.showCoin(thisSupplier_ActualBuyPriceSum);
                    thisSupplier_ExpectBuyPriceSum = thisSupplier_ActualBuyPriceSum = 0;
                }
                else e.Item.FindControl("tr_xiaoji").Visible = false;
                //((TextBox)e.Item.FindControl("tb_id")).Text = rowData.id.ToString();
                //((TextBox)e.Item.FindControl("tb_BuyPrice")).Text = StringPlus.showCoin(rowData.SellPrice);
                //((TextBox)e.Item.FindControl("tb_ActualAmount")).Text = rowData.ActualAmount.ToString();
                //((TextBox)e.Item.FindControl("tb_ActualGiftNumber")).Text = rowData.ActualGiftNumber.ToString();
                //
                ActualBuyPriceSum += rowData.ActualAmount * DataTypePlus.requestDecimal(rowData.BuyPrice);
            }
        }

     
        protected void bt_HasBuy_Click(object sender, EventArgs e)
        {
            bt_HasBuy.Enabled = false;
            model.Ischeck = 3;
            bll.Update(model);
            Master.SubmitOK("本订单已通过");
        }

        protected void bt_HasBuy0_Click(object sender, EventArgs e)
        {
            ActualBuyPriceSum = 0;
            string sql = "OrdersID=" + Master.id;

            if (IsDelivery != "") sql += " and IsDelivery=" + IsDelivery;

            sql += " order by IsDelivery desc,SupplierID, HasBuy";
            listModelV_Orders_Goods = bllV_Orders_Goods.GetModelList(sql);
            foreach (V_Orders_Goods vog in listModelV_Orders_Goods)
            {
                Model.Orders_Goods modelOrders_Goods = bllOrders_Goods.GetModel(vog.id);
               
                modelOrders_Goods.ActualAmount = 0;

                modelOrders_Goods.ActualBuyPrice = 0;
                modelOrders_Goods.ActualBuyPriceSum = 0;
                modelOrders_Goods.ActualBuyDiscount = 0;
                modelOrders_Goods.ActualBuyGiftNumber = 0;
                //Sell
                modelOrders_Goods.ActualPrice = 0;
                modelOrders_Goods.ActualPriceSum = 0;
                modelOrders_Goods.ActualDiscount = 0;
                modelOrders_Goods.ActualGiftNumber = 0;
                modelOrders_Goods.HasBuy = false;
                modelOrders_Goods.ActualPay = 0;
                bllOrders_Goods.Update(modelOrders_Goods);//先更新，为了Pay获取数据用
            }



            model.ActualBuyPriceSum = 0;
            model.ActualPriceSum = 0;
            model.ActualPay_Delivery = 0;
            model.ActualPay = 0;
            model.HasPay = false;
            model.HasPay_Delivery = false;
            model.IsBuyMoneyOK_Delivery = model.ExpectPay_Delivery == model.ActualPay_Delivery;
            model.State = (int)EnumHelper.OrdersState.Buying;
            model.Ischeck = 2;
            bll.Update(model);

            BLL.User_Operate_Log.Add(Security.GetCurrentUserID(), EnumHelper.TableName.Orders, Master.id, EnumHelper.OperateType.Update, "订单" + Master.id + "完成");

            Master.SubmitOK("本订单已退回");
        }

    }
}